
#include "TelephoneInfo.h"
#include <stdio.h>
#include <string.h>

void init_info(telephonebook *t)
{
    InitMyString(&t->info->name);
    InitMyString(&t->info->phone);
    t->size = 0;
}

void insert_user(telephonebook *t)
{
    if (t->size >= MAX_USERS)
    {
        printf("通讯录已满，无法添加新用户。\n");
        return;
    }
    printf("请输入新用户的名字\n");
    scanf("%s", t->info[t->size].name.str);
    printf("请输入新用户的电话号码\n");
    scanf("%s", t->info[t->size].phone.str);
    t->size++;
}

void output_user(telephonebook *t, int i)
{
    printf("用户姓名：");
    PrintStr(&t->info[i].name, false);
    printf("    用户号码：");
    PrintStr(&t->info[i].phone, true);
}

user *find_user_name(telephonebook *t, MyStr *name)
{
    for (int i = 0; i < t->size; i++)
    {
        if (IsStrEqual(&t->info[i].name, name))
        {
            return &t->info[i];
        }
    }
    return NULL;
}

void rm_info_by_name(telephonebook *t, MyStr *name)
{
    for (int i = 0; i < t->size; i++)
    {
        if (IsStrEqual(&t->info[i].name, name))
        {
            for (int j = i; j < t->size - 1; j++)
            {
                t->info[j] = t->info[j + 1];
            }
            t->size--;
            i--;
        }
    }
}

void modify_info(telephonebook *t,user *u)
{
    telephonebook new_information;
    init_info(&new_information);
    insert_user(&new_information);
    InsertStr(&u->name, &new_information.info[0].name);
    InsertStr(&u->phone, &new_information.info[0].phone);
}